import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    EditDtoExtensionsItem,
    Stock_TRBaseListDto,
    DataSourceType,
    StockZT_InventoryBillItemServiceProxy,
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';

interface SelectedItems {
    name: string;
    code: string;
    id: string;
}
@Component({
    selector: 'toolBillItemDetailModal',
    templateUrl: './tool-bill-item-detail-modal.component.html'
})
export class ToolBillItemDetailModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _inventorybillService: StockZT_InventoryBillItemServiceProxy,
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    editDto;
    extensions: Array<EditDtoExtensionsItem>;
    optionAll;

    ngOnInit() {}

    // getBaseinfo(event)
    // {
    //   this.active=true;
    //   this.modal.show();

    // }
    show(record: Stock_TRBaseListDto): void {
        this.active = true;

        // if (record.f_DataSource == DataSourceType.采购入库 || record.f_DataSource == DataSourceType.数据初始化) {
        //     this._intobillService.getMiliIntoBillItemEditExtension(record.f_BillItemId).subscribe(result => {
        //         let optionAll = {};

        //         if (result.selects) {
        //             result.selects.forEach(el => {
        //                 optionAll[el.label] = el.value;
        //             });
        //         }

        //         this.optionAll = optionAll;

        //         this.editDto = result.dto;
        //         this.extensions = result.items;
        //         this.extensions.forEach(el => {
        //             el.readonly = true;
        //         });
        //         this.modal.show();
        //     });
        // }
        if (record.f_DataSource == DataSourceType.盘点) {
            this._inventorybillService.getMiliInventoryBillItemEditExtension(record.f_BillItemId).subscribe(result => {
                let optionAll = {};

                if (result.selects) {
                    result.selects.forEach(el => {
                        optionAll[el.label] = el.value;
                    });
                }

                this.optionAll = optionAll;

                this.editDto = result.dto;
                this.extensions = result.items;
                this.extensions.forEach(el => {
                    el.readonly = true;
                });
                this.modal.show();
            });
        }
        //   if(record.f_DataSource==DataSourceType.调拨入库||record.f_DataSource==DataSourceType.调拨出库)
        //   {
        //         this._transferbillService.getMiliOutBillItemEditExtension(record.f_BillItemId).subscribe(result => {
        //         let optionAll = {};

        //         if (result.selects) {
        //             result.selects.forEach(el => {
        //                 optionAll[el.label] = el.value;
        //             });
        //         }

        //         this.optionAll = optionAll;

        //         this.editDto = result.dto;
        //         this.extensions = result.items;
        //         this.extensions.forEach(el => {
        //             el.readonly = true;
        //         });
        //         this.modal.show();
        //     });
        //     }

        if (
            record.f_DataSource == DataSourceType.领用出库 ||
            record.f_DataSource == DataSourceType.转移出库 ||
            record.f_DataSource == DataSourceType.转移入库 ||
            record.f_DataSource == DataSourceType.领用入库
        ) {
            // this._outbillService.getMiliOutBillItemEditExtension(record.f_BillItemId).subscribe(result => {
            //     let optionAll = {};

            //     if (result.selects) {
            //         result.selects.forEach(el => {
            //             optionAll[el.label] = el.value;
            //         });
            //     }

            //     this.optionAll = optionAll;

            //     this.editDto = result.dto;
            //     this.extensions = result.items;
            //     this.extensions.forEach(el => {
            //         el.readonly = true;
            //     });
            //     this.modal.show();
            // });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
